//related to picker3.ui
var page = sm("do_Page");
var app = sm("do_App");
var storage = sm("do_Storage");
var nf = sm("do_Notification");
var root = ui("$");
var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");

var dateTabs = mm("do_ListData");
var do_SegmentView_date = ui("do_SegmentView_date");
do_SegmentView_date.bindItems(dateTabs);

//遮罩显示
var animMaskShow = mm("do_Animator");
var propsMS = {bgColor:"00000077"};
animMaskShow.append(360,propsMS,"EaseOut");
//面板显示
var animPanelShow = mm("do_Animator");
var propsPS = {y:460};
animPanelShow.append(360,propsPS,"EaseOut");
//遮罩隐藏
var animMaskHide = mm("do_Animator");
var propsMH = {bgColor:"00000000"};
animMaskHide.append(360,propsMH,"EaseIn");
//面板隐藏
var animPanelHide = mm("do_Animator");
var propsPH = {y:960};
animPanelHide.append(360,propsPH,"EaseIn");

//picker
var labtit = ui("labtit");
var pickerbg = ui("pickerbg");//遮罩
var btnsure = ui("labButton");//确定按钮
var pickerbox = ui("do_ALayout_1");

var hourIntervals = 1; //时间间隔
var minuteIntervals = 0; //分钟间隔
var hours = []; 
function getHours(data) {
	hours = [];
	if (data.hourIntervals) {
		hourIntervals = data.hourIntervals;
	}
	if (data.minuteIntervals) {
		minuteIntervals = data.minuteIntervals;
	}
	for (var i = 7; i < 22; i += hourIntervals) {
		if (minuteIntervals == 0) {
			hours.push({"hour":i + ":00","selected":0,"bgColor":"00000000","val":i});
		} else if (minuteIntervals == 0.30) {
			hours.push({"hour":i + ":00","selected":0,"bgColor":"00000000","val":i});
			hours.push({"hour":i + ":30","selected":0,"bgColor":"00000000","val":i+0.3});
		}
	}
	hourGrids.removeAll();
	hourGrids.addData(hours);
	do_GridView_hour.refreshItems();
	return hours;
}
var hourGrids = mm("do_ListData");
var do_GridView_hour = ui("do_GridView_hour");
do_GridView_hour.bindItems(hourGrids);

var baseId,typeId;
var param= {};
var nowHour = 8;
var flag = 1;//1不需要判断时间是否被占用（目前），2需要判断时间是否被占用

page.on("times",function(data){
	pickerbg.visible = true;
	pickerbg.animate(animMaskShow);
	pickerbox.animate(animPanelShow);
})

page.on("getTimes",function(data){
	hours = getHours(data);
	flag = data.flag;
	typeId = data.typeId;
	if (flag == 2) {
		baseId = data.baseId;
	}
	qqHttp.post(homemakingUtil.url.time.findByDate, {baseId:baseId}, function(data1) {
		do_SegmentView_date.index = 0;
		var oDate = new Date();
		nowHour = Number(data1.nowHour+"."+oDate.getMinutes());
		param.serviceDateStr = data1.serviceDateStr;
		dateTabs.removeAll();
		dateTabs.addData(data1.dateList);
		dateTabs.updateOne(0, {"date":data1.dateList[0].date,"dateStr":data1.dateList[0].dateStr,"border":"ff7e00FF,1,4","fontColor":"ff7e00FF"});
		do_SegmentView_date.refreshItems({});
		
		var indexs = [];
		var data_temp = hourGrids.getRange(0);
		for (var i = 0; i < hours.length; i++) {
			if (hours[i].val <= nowHour) {
				//hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":0,"bgColor":"f2f3f6FF"});
				indexs.push(i);
			} else if (typeId != homemakingUtil.typeId.SongShui) {
				//除了送水，其他的当天不能预约
				hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":0,"bgColor":"f2f3f6FF"});
			}
		}
		if (indexs.length > 0) {
			hourGrids.removeData(indexs);
		}
		if (flag == 2) {
			if (data1.selectHourList) {
				var selectHourList = data1.selectHourList;
				for (var i=0; i<selectHourList.length; i++) {
					for (var j=0; j<hours.length; j++) {
						if (hours[j].hour == selectHourList[i].serviceHourStr) {
							hourGrids.updateOne(j, {"hour":data_temp[j].hour,"selected":0,"bgColor":"f2f3f6FF"});
							break;
						}
					}
				}
			}
		}
		if (typeId == homemakingUtil.typeId.SongShui) {
			hourGrids.addOne({"hour":"马上预定","selected":0,"bgColor":"00000000","val":100},0);
		}
		do_GridView_hour.refreshItems();
	});
})

//确定事件
btnsure.on("touch",function(){
	if (!param || !param.serviceDateStr || !param.serviceHourStr) {
		nf.toast("请选择时间");
		return;
	}
	pickerbox.animate(animPanelHide);
	pickerbg.animate(animMaskHide,function(){
		pickerbg.visible = false;
	});
	page.fire("refreshTime",param);
});

//遮罩事件
pickerbg.on("touch","",300,function(){
	pickerbox.animate(animPanelHide);
	pickerbg.animate(animMaskHide,function(){
		pickerbg.visible = false;
	});
});

//防穿透
pickerbox.on("touch",function(){})

do_SegmentView_date.on("indexChanged", function(index) {
	var data_temp = dateTabs.getRange(0);
	param.serviceDateStr = data_temp[index].date;
	param.serviceHourStr = '';
    for (var i = 0; i < dateTabs.getCount(); i++) {
        if (i == index) {
        	dateTabs.updateOne(i, {
                "date" : data_temp[i].date,
                "dateStr" : data_temp[i].dateStr,
                "border" : "ff7e00FF,1,4",
                "fontColor" : "ff7e00FF"
            });
        } else {
        	dateTabs.updateOne(i, {
            	"date" : data_temp[i].date,
            	"dateStr" : data_temp[i].dateStr,
                "border" : "f2f3f6FF,1,4",
                "fontColor" : "949eaaFF"
            });
        }
    }
    do_SegmentView_date.refreshItems();
    refreshHours(index,data_temp[index].date);
});

//订阅GridView的touch点击事件
do_GridView_hour.on("touch", function(data) {
	//nf.alert("第" + (data + 1) + "条数据被点中");
	var data_temp = hourGrids.getRange(0);
	if (data_temp[data].bgColor == 'f2f3f6FF') {
		return;
	}
	param.serviceHourStr = data_temp[data].hour;
	for (var i = 0; i < hourGrids.getCount(); i++) {
        if (i == data) {
        	hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":1,"bgColor":"00000000"});
        } else {
        	hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":0,"bgColor":data_temp[i].bgColor});
        }
    }
	do_GridView_hour.refreshItems();
})

/* 刷新时间 */
function refreshHours(index,serviceDateStr){
	if (flag == 2) {
		qqHttp.post(homemakingUtil.url.time.findByDate, {baseId:baseId,serviceDateStr:serviceDateStr}, function(data) {
			hourGrids.removeAll();
			hourGrids.addData(hours);
			var data_temp = hourGrids.getRange(0);
			if (index == 0) {
				for (var i = 0; i < hours.length; i++) {
					if (hours[i].val <= data.nowHour) {
						hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":0,"bgColor":"f2f3f6FF"});
					} else if (typeId != homemakingUtil.typeId.SongShui) {
						//除了送水，其他的当天不能预约
						hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":0,"bgColor":"f2f3f6FF"});
					}
				}
			}
			if (data.selectHourList) {
				var selectHourList = data.selectHourList;
				for (var i=0; i<selectHourList.length; i++) {
					for (var j=0; j<hours.length; j++) {
						if (hours[j].hour == selectHourList[i].serviceHourStr) {
							hourGrids.updateOne(j, {"hour":data_temp[j].hour,"selected":0,"bgColor":"f2f3f6FF"});
							break;
						}
					}
				}
			}
			do_GridView_hour.refreshItems();
		});
	} else {
		hourGrids.removeAll();
		hourGrids.addData(hours);
		var data_temp = hourGrids.getRange(0);
		if (index == 0) {
			var indexs = [];
			for (var i = 0; i < hours.length; i++) {
				if (hours[i].val <= nowHour) {
					//hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":0,"bgColor":"f2f3f6FF"});
					indexs.push(i);
				} else if (typeId != homemakingUtil.typeId.SongShui) {
					//除了送水，其他的当天不能预约
					hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":0,"bgColor":"f2f3f6FF"});
				}
			}
			if (indexs.length > 0) {
				hourGrids.removeData(indexs);
			}
			if (typeId == homemakingUtil.typeId.SongShui) {
				hourGrids.addOne({"hour":"马上预定","selected":0,"bgColor":"00000000","val":100},0);
			}
		}
		do_GridView_hour.refreshItems();
	}
}